package com.xd.study.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xd.study.R;

/**
 * Created by DGF on 2015/4/30.
 */
public class DialogActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);

        Button btnPositive = (Button)this.findViewById(R.id.dialog_btn_positive);
        Button btnSingleSelect = (Button)this.findViewById(R.id.dialog_btn_singleselect);
        Button btnMultiSelect = (Button)this.findViewById(R.id.dialog_btn_multiselect);
        Button btnList = (Button)this.findViewById(R.id.dialog_btn_list);
        final Button btnCustom = (Button)this.findViewById(R.id.dialog_btn_custom);

        btnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("确认对话框");
                builder.setIcon(R.drawable.ic_launcher);
                builder.setMessage("对话框内容");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        btnSingleSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("单选按钮对话框");
                builder.setIcon(R.drawable.ic_launcher);

                final String[] singleItems = {"男", "女"};

                builder.setSingleChoiceItems(singleItems,1,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "选择了："+singleItems[which], Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        btnMultiSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("多选按钮对话框");
                builder.setIcon(R.drawable.ic_launcher);

                final String[] multiItems = {"游泳","爬山","跳舞","唱歌","篮球"};
                builder.setMultiChoiceItems(multiItems, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked){
                            Toast.makeText(DialogActivity.this, "我喜欢 "+multiItems[which], Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(DialogActivity.this, "我不喜欢" + multiItems[which], Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        btnList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("列表对话框");
                builder.setIcon(R.drawable.ic_launcher);

                final String[] listItems = {"程序员", "测试", "策划", "市场", "人力资源"};

                builder.setItems(listItems, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "点击了 " + listItems[which], Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        btnCustom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(DialogActivity.this);
                View view = View.inflate(DialogActivity.this, R.layout.dialog_layout, null);

                Button btnOK = (Button)view.findViewById(R.id.dialog_btn_ok);
                btnOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(DialogActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("自定义对话框");
                builder.setIcon(R.drawable.ic_launcher);
                builder.setView(view);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }
}
